#用来通知节庆或者活动

load 'tutem_utils.rb'
load 'send-sms.rb'

def awardUserPoints points, code, title, uid = 1, openid = 'oqlyX6deLDP2EWlk0qZgWCU7wH6I'
    rec = {}
    rec.store 'uid', uid
    rec.store 'bonus', points
    rec.store 'code', code
    rec.store 'channel', title
    rec.store 'brief', title
    rec.store 'title', title
    rec.store 'mark', title
    rec.store 'weworkNotice', false
    rec.store 'mpNotice', false
    bill_id = award_points_to_user rec
    if bill_id > 0 
        url = 'https://mta.totembio.cn/apps/loyalty/lottery'
        miniprogram = {}
        totem_points_change_notice openid, title, points, url, miniprogram
    end
end

def mpWakeupUsers code='LTR-2023-09-25-WAKEUP', tips='积分(庆中秋福利)点击参加抽奖'

    sql = "select u.uid, w.openid as openid, u.real_name, u.phone, u.integral, w.subscribe from tutem_mall.eb_user u left join tutem_mall.eb_wechat_user w on (u.uid = w.uid) where u.status = 1 and u.uid not     in (select uid from tutem_mall.eb_user_lottery_results) and w.subscribe = 1 and w.openid is not null and u.uid not in (select uid from tutem_mall.eb_user_bill where link_id like '#{code}' );"
    queryRds(sql).each do |rec|
        uid = rec['uid']
        openid = rec['openid']
        #awardUserPoints 100, code, tips, uid, openid
    end

end
#mpWakeupUsers

#url = 'https://mta.totembio.cn/apps/loyalty/lottery'
#miniprogram = {}
#totem_points_change_notice 'oqlyX6deLDP2EWlk0qZgWCU7wH6I', '空气加钱', 100, url, miniprogram

def smsWakeupUsers points, code='LTR-2023-09-25-WAKEUP-29', title='庆中秋积分福利', tips='积分(庆中秋福利)点击参加抽奖'
    sql = "SELECT u.uid, u.real_name as name, COALESCE(u.phone,o.user_phone ) AS phone, u.pay_count, u.integral FROM
    tutem_mall.eb_user u LEFT JOIN ( SELECT uid, user_phone FROM tutem_mall.eb_store_order WHERE paid = 1 GROUP BY uid) o ON u.uid = o.uid
    WHERE u.uid NOT IN ( SELECT uid FROM tutem_mall.eb_user_bill WHERE link_id LIKE '%WAKEUP%')
    AND u.status = 1 AND u.uid NOT IN ( SELECT uid FROM tutem_mall.eb_user_lottery_results)
    AND (u.phone IS NOT NULL OR u.pay_count >= 1) order by phone desc"
    count = 0
    queryRds(sql).each do |rec|
        phone = rec['phone'].strip
        uid = rec['uid']
        name = rec['name']
        if phone[0] == '1' && phone.length == 11
            count += 1
            rec = {}
            rec.store 'uid', uid
            rec.store 'bonus', points
            rec.store 'code', code
            rec.store 'channel', title
            rec.store 'brief', title
            rec.store 'title', title
            rec.store 'mark', title
            rec.store 'weworkNotice', false
            rec.store 'mpNotice', false
            rec.store 'mpsRequired', false
            bill_id = award_points_to_user rec
            if bill_id > 0
                puts "award uid##{uid} #{name} and sms to #{phone}"
                sms20230928 phone 
            end
        end
    end
    puts "count: #{count}"
end
#smsWakeupUsers 199
